package tianqi.controller.order;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tianqi.entity.Order;
import tianqi.entity.OrderDetail;
import tianqi.entity.ShareOrder;
import tianqi.entity.ShopCar;
import tianqi.entity.UserY;
import tianqi.service.order.impl.OrderServiceImpl;
import tianqi.service.shop.impl.ShopCarServiceImpl;
import tianqi.utils.TQMoneySmallException;
import tianqi.utils.TQStockException;

@Controller
public class OrderController {

	@Autowired
	private OrderServiceImpl orderServiceImpl;

	@Autowired
	private ShopCarServiceImpl shopCarServiceImpl;
	/**
	 * 由订单确认   跳转至订单提交页
	 * @param order
	 * @param req
	 * @param res
	 * @param m
	 * @return
	 */
	@RequestMapping("/orderSubmit")
	public String orderSubmit(Order order, HttpServletRequest req, HttpServletResponse res, Model m) {
		UserY userY = (UserY) req.getSession().getAttribute("user");
		if (userY == null) {
			return "redirect:show.tq";
		}
		List<ShopCar> queryCars = shopCarServiceImpl.queryCars(req, res);
		List<OrderDetail> oList = new ArrayList<OrderDetail>();
		for (ShopCar shopCar : queryCars) {
			OrderDetail orderDetail = new OrderDetail();
			orderDetail.setOnum(shopCar.getNum());
			orderDetail.setProCall(shopCar.getPro().getProCall());
			orderDetail.setProId(shopCar.getIdPro());
			orderDetail.setProImg(shopCar.getDetailImg());
			orderDetail.setProSpec(shopCar.getSpec());
			orderDetail.setMoney(Integer.valueOf(shopCar.getNum()) * Double.valueOf(shopCar.getPro().getYgPrice()));
			oList.add(orderDetail);
		}

		order.setUserId(userY.getUid());
		order.setOrderState("0");
		try {
			orderServiceImpl.saveOrder(order, oList, req, res);
		} catch (TQStockException e) {

			m.addAttribute("msg", OrderServiceImpl.error);
		}
		m.addAttribute("msg", OrderServiceImpl.error);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		// 获取系统当前时间
		List<Order> queryOrderByUid = orderServiceImpl.queryOrderByUid(userY);
		for (Order order2 : queryOrderByUid) {
			order2.setDate(df.format(order2.getOrderTime()));
		}
		m.addAttribute("order", queryOrderByUid);
		m.addAttribute("moneys", orderServiceImpl.queryMoneyByUid(userY));

		return "orderSubmit";
	}
	/**
	 * 根据订单ID  删除订单
	 * @param id
	 * @param req
	 * @param res
	 * @param m
	 * @return
	 */
	@RequestMapping("/deleteOrderById")
	public String deleteOrderById(String id, HttpServletRequest req, HttpServletResponse res, Model m) {
		UserY userY = (UserY) req.getSession().getAttribute("user");
		if (userY == null) {
			return "redirect:show.tq";
		}
		Order order = new Order();
		order.setOrderId(Integer.valueOf(id));
		orderServiceImpl.deleteOrderById(order);
		m.addAttribute("msg", OrderServiceImpl.error);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		// 获取系统当前时间
		List<Order> queryOrderByUid = orderServiceImpl.queryOrderByUid(userY);
		for (Order order2 : queryOrderByUid) {
			order2.setDate(df.format(order2.getOrderTime()));
		}
		m.addAttribute("order", queryOrderByUid);
		m.addAttribute("moneys", orderServiceImpl.queryMoneyByUid(userY));
		return "orderSubmit";
	}

	/**
	 * 订单支付
	 * 
	 * @param id
	 * @param req
	 * @param res
	 * @param m
	 * @return
	 */
	@RequestMapping("/orderPay")
	public String orderPay(String orderId, HttpServletRequest req, HttpServletResponse res, Model m) {
		UserY userY = (UserY) req.getSession().getAttribute("user");
		if (userY == null) {
			return "redirect:show.tq";
		}
		try {
			orderServiceImpl.savePay(orderId, userY);
		} catch (TQStockException e) {
			System.out.println("库存不足   抛出异常");
		} catch (TQMoneySmallException e) {
			m.addAttribute("msg", OrderServiceImpl.error);
			// 如果 用户余额不足 跳转至，充值页面
			return "recharge";
		}

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		m.addAttribute("msg", OrderServiceImpl.error);
		List<Order> queryOrderByUid = orderServiceImpl.queryOrderByUid(userY);
		for (Order order2 : queryOrderByUid) {
			order2.setDate(df.format(order2.getOrderTime()));
		}
		m.addAttribute("order", queryOrderByUid);
		m.addAttribute("moneys", orderServiceImpl.queryMoneyByUid(userY));
		return "orderSubmit";
	}
	/**
	 * 转发到  订单页
	 * @param req
	 * @param res
	 * @param m
	 * @return
	 */
	@RequestMapping("/tosubmitOrder")
	public String tosubmitOrder(HttpServletRequest req, HttpServletResponse res, Model m) {
		UserY userY = (UserY) req.getSession().getAttribute("user");
		if (userY == null) {
			return "redirect:show.tq";
		}
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		m.addAttribute("msg", OrderServiceImpl.error);
		List<Order> queryOrderByUid = orderServiceImpl.queryOrderByUid(userY);
		for (Order order2 : queryOrderByUid) {
			order2.setDate(df.format(order2.getOrderTime()));
		}
		m.addAttribute("order", queryOrderByUid);
		m.addAttribute("moneys", orderServiceImpl.queryMoneyByUid(userY));
		return "orderSubmit";
	}
	/**
	 * 充值模块
	 * @param orderId
	 * @param req
	 * @param res
	 * @param m
	 * @return
	 */
	@RequestMapping("/recharge")
	public String recharge(String orderId, HttpServletRequest req, HttpServletResponse res, Model m) {
		UserY userY = (UserY) req.getSession().getAttribute("user");
		if (userY == null) {
			return "redirect:show.tq";
		}
		m.addAttribute("user", userY);
		return "recharge";
	}

	/**
	 * 用户晒单模块
	 * 
	 * @param orderId
	 * @param req
	 * @param res
	 * @param m
	 * @return
	 */
	@RequestMapping("/shareOrder")
	public String shareOrder(String orderId, HttpServletRequest req, HttpServletResponse res, Model m) {
		UserY userY = (UserY) req.getSession().getAttribute("user");
		if (userY == null) {
			return "redirect:show.tq";
		}
		Order order = new Order();
		order.setOrderId(Integer.valueOf(orderId));
		m.addAttribute("od", orderServiceImpl.queryOrderDetail(order));
		m.addAttribute("user", userY);
		return "shareOrder";
	}

	/**
	 * 提交晒单内容
	 * 
	 * @param orderId
	 * @param req
	 * @param res
	 * @param m
	 * @return
	 */
	@RequestMapping("/submit")
	@ResponseBody
	public String submit(ShareOrder o, HttpServletRequest req, HttpServletResponse res, Model m) {
		UserY userY = (UserY) req.getSession().getAttribute("user");
		if (userY == null) {
			return "redirect:show.tq";
		}
		o.setSoUname(userY.getUname());
		o.setSoTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		Integer saveShareOrder = orderServiceImpl.saveShareOrder(o);
		if (saveShareOrder > 0) {
			return "true";
		}
		return "false";
	}
	/**
	 * 用于展示此用户   的晒单记录
	 * @param req
	 * @param res
	 * @param m
	 * @return
	 */
	@RequestMapping("/myShareOrder")
	public String myshareOrder(HttpServletRequest req, HttpServletResponse res, Model m) {
		UserY userY = (UserY) req.getSession().getAttribute("user");
		if (userY == null) {
			return "redirect:show.tq";
		}
		List<ShareOrder> querySoSByUname = orderServiceImpl.querySoSByUname(userY);

		for (ShareOrder shareOrder : querySoSByUname) {
			if (shareOrder.getSoImg().indexOf(";") > 0) {
				shareOrder.setSplit(shareOrder.getSoImg().split(";"));
			} else {
				shareOrder.setSplit(new String[] { shareOrder.getSoImg() });
			}
		}
		m.addAttribute("sos", querySoSByUname);
		m.addAttribute("user", userY);
		return "myShareOrder";
	}

}
